package com.foo.hibernate;

import javax.ejb.Stateless;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


import com.foo.hibernate.RealTimeData;
import com.foo.DataProvider.ProviderInterface;
import com.foo.DataSender.DataSender;


import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;


import javax.persistence.Query;

/**
 * Session Bean implementation class AnalyzedHibernate
 */
@Stateless
public class AnalyzedHibernate implements ProviderInterface{

	int total =0 ;
	Query getanalyzeddata = null;
	List<RealTimeData> analyzeddatalist = null;
	String analyzedmessage="";;
	DataSender datasender = new DataSender();
	
    /**
     * Default constructor. 
     */
    public AnalyzedHibernate() 
    {
    System.out.println("Alalyzed Hibernate Constructor.");
    }
    
	@SuppressWarnings("unchecked")
	@Override
	public void getData(String exampleName, String value, Timestamp timestamp, Date fromdate, Date todate) 
	{			
		System.out.println("Entering server function Analyzed Data with parameters " +exampleName);
		
		if(exampleName.equals("TopVolume"))
		{
			SessionFactory SF = new Configuration().configure().buildSessionFactory();
			Session session = SF.openSession();
			session.beginTransaction();
			
			System.out.println("In top volume condition.");
			analyzedmessage="";
			analyzeddatalist = (List<RealTimeData>)((session.createSQLQuery("select distinct * from realtimestockdata order by volume desc")
					.addEntity(RealTimeData.class).setMaxResults(25)).list());
				
			session.getTransaction().commit();
			session.close();
			
			System.out.println("here!");
			
			int listsize = analyzeddatalist.size();
			System.out.println("list size "+listsize);
			for(int k=0;k<listsize;k++)
			{
				analyzedmessage += analyzeddatalist.get(k).getTicker_symbol() + "," +analyzeddatalist.get(k).getOpen_value()+","+analyzeddatalist.get(k).getHigh_value()
				+","+analyzeddatalist.get(k).getLow_value()+","+analyzeddatalist.get(k).getClose_value()+","+analyzeddatalist.get(k).getVolume()+"," + analyzeddatalist.get(k).getPerc_change() + ",";
			}

			
			
		}
		else if(exampleName.equals("TopIncr"))
		{
			SessionFactory SF = new Configuration().configure().buildSessionFactory();
			Session session = SF.openSession();
			session.beginTransaction();
			
			System.out.println("In top incr.");
			analyzedmessage="";
			analyzeddatalist = (List<RealTimeData>)((session.createSQLQuery("select distinct * from realtimestockdata Group by ticker_symbol order by perc_change desc")
					.addEntity(RealTimeData.class).setMaxResults(25)).list());
				
			session.getTransaction().commit();
			session.close();
			
			int listsize = analyzeddatalist.size();
			System.out.println("list size "+listsize);
			for(int k=0;k<listsize;k++)
			{
				analyzedmessage += analyzeddatalist.get(k).getTicker_symbol() + "," +analyzeddatalist.get(k).getOpen_value()+","+analyzeddatalist.get(k).getHigh_value()
				+","+analyzeddatalist.get(k).getLow_value()+","+analyzeddatalist.get(k).getClose_value()+","+analyzeddatalist.get(k).getVolume()+"," + analyzeddatalist.get(k).getPerc_change() + ",";
			}
		}
		else if(exampleName.equals("TopDecr"))
		{
			SessionFactory SF = new Configuration().configure().buildSessionFactory();
			Session session = SF.openSession();
			session.beginTransaction();
			
			System.out.println("In top decr.");
			analyzedmessage="";
			analyzeddatalist = (List<RealTimeData>)((session.createSQLQuery("select distinct * from realtimestockdata Group by ticker_symbol order by perc_change")
					.addEntity(RealTimeData.class).setMaxResults(25)).list());
				
			session.getTransaction().commit();
			session.close();
			
			System.out.println("here!");
			
			int listsize = analyzeddatalist.size();
			System.out.println("list size "+listsize);
			for(int k=0;k<listsize;k++)
			{
				analyzedmessage += analyzeddatalist.get(k).getTicker_symbol() + "," +analyzeddatalist.get(k).getOpen_value()+","+analyzeddatalist.get(k).getHigh_value()
				+","+analyzeddatalist.get(k).getLow_value()+","+analyzeddatalist.get(k).getClose_value()+","+analyzeddatalist.get(k).getVolume()+"," + analyzeddatalist.get(k).getPerc_change() + ",";
			}
		}
		else
		{
			analyzedmessage="";
		}
		datasender.sendMessage("Analyzed",analyzedmessage);
		System.out.println("The message " +analyzedmessage);
	}
}
